﻿using System.Configuration;
using System.Windows.Forms;
using EO.WebBrowser;
using EO.WebBrowser.WinForm;
using Citi.Helpers;
using System.IO;
using System;
using Citi.SecurityWebBrowser;

namespace Citi.ProcureToPay.WindowsUi.P2PExtraction
{
    public class Extract : EOWebBrowserUtil
    {
        public Extract(WebControl webControl, string reportName, string startUrl, string ssoUrl, string additionalUrls, string userLogin, string pathToSave,
            string parameters, bool newWindowNoAction, Form form = null)
            : base(webControl, reportName, startUrl, ssoUrl, userLogin, pathToSave, parameters, newWindowNoAction, form)
        { }

        public override void StartExtractionLogged()
        {
            WebBrowser.NewWindow += webBrowser_NewWindowOpenTargetUrl;

            Wait(100);

            Log("Opening Discoverer Viewer");
            ClickOnLink("CITI BR BRL DISCOVERER VIEWER");

            Log("Expanding All Reports");
            ClickOnLink("Expand All");

            Log(string.Format("Opening report '{0}'", ReportName));
            ClickOnLink(ReportName);

            FillParameters();

            Wait(4000);

            ExportReport();
        }

        private void ClickOnLink(string linkInnerHtml)
        {
            var found = false;

            while (!found)
            {
                found = (bool)WebBrowser.EvaluateScript(string.Format(JavascriptQuery.ClickOnLink, linkInnerHtml));
                Wait(200);
            }

            Wait(2500);
        }

        private void ExportReport()
        {
            Log("Waiting report generation");
            WebBrowser.WaitUntilExist("document.getElementById('export')", 3600000);
            Wait(4000);

            Log("Report generated. Clicking on Export Button");
            ClickOnElement("export", null, null, null);

            Log("Waiting to export report");
            WebBrowser.WaitUntilExist("document.getElementById('ExportForm')", 600000);
            WebBrowser.EvaluateScript("submitForm('ExportForm',1,{'event':'ExportData'});return false");
            Wait(4000);

            Log("Clicking on Save button");
            ClickOnLink("Click to view or save");
        }

        private void FillParameters()
        {
            Log("Getting parameters");
            var parameters = GetParametersDictionary();

            WebBrowser.WaitUntilExist("document.getElementById('parametersForm')", 600000);

            Log("Filling parameters");

            foreach (var parameter in parameters)
            {
                var value = !File.Exists(parameter.Value)
                    ? parameter.Value
                    : string.Format("'{0}'", string.Join("','", File.ReadAllLines(parameter.Value)));

                Log((string)WebBrowser.EvaluateScript(string.Format(JavascriptQuery.FillParameter, parameter.Key, value)));
            }

            Log("Clicking on Generate Button");
            WebBrowser.EvaluateScript("paramsbi_selectParams();");
        }
    }
}